/**
 * Created by jianglinjie on Thu Sep 21 2017
 * @flow
 */
'use strict';

import Router from 'koa-router';
import RegisterService from '../services/register';
import AuthService from '../services/auth';
import PasswordService from '../services/password';
import UserProfileService from '../services/userProfile';
import { RegisterType } from '../constants';
import { LoginType } from '../constants';
import { createSucceedBody } from '../utils/BodyUtils';
import { authenticationMiddleware, getCurrentUidFromCtx } from '../utils/TokenUtils';
import { createError } from '../utils/ErrorUtils';
import { ErrorCode } from '../constants';

const router = new Router();

//注册
router.post('/', async (ctx) => {
  let registerType = ctx.request.query.type || RegisterType.USERNAME_PWD;
  let result = await RegisterService.register(registerType, ctx.request.body);
  ctx.response.status = 201;
  ctx.response.body = createSucceedBody('注册成功', result);
});

//登录
router.post('/actions/login', async (ctx) => {
  let loginType = ctx.request.query.type || LoginType.ACCOUNT_ENCRYPTPWD;
  let result = await AuthService.login(loginType, ctx.request.body);
  ctx.response.status = 200;
  ctx.response.body = createSucceedBody('登录成功', result);
});

//登出
router.post('/actions/logout', async (ctx) => {
  let result = await AuthService.logout(ctx.request.body);
  ctx.response.status = 200;
  ctx.response.body = createSucceedBody('登出成功', result);
});

//重置密码
router.put('/password/actions/reset', async (ctx) => {
  let result = await PasswordService.resetPassword(ctx.request.body);
  ctx.response.status = 200;
  ctx.response.body = createSucceedBody('修改成功', result);
});

//修改密码
router.put('/:uid/password/actions/modify', authenticationMiddleware, async (ctx) => {
  let uid = getCurrentUidFromCtx(ctx);
  if (ctx.params.uid !== uid) {
    throw createError(403, '无权限', ErrorCode.ACCESS_DENY);
  }

  let result = await PasswordService.modify({ uid: ctx.params.uid, ...ctx.request.body });
  ctx.response.status = 200;
  ctx.response.body = createSucceedBody('修改成功', result);
});

router.get('/:uid', async (ctx) => {
  let result = await UserProfileService.getUser({ uid: ctx.params.uid});
  ctx.response.status = 200;
  ctx.response.body = createSucceedBody('获取成功', result);
});

router.patch('/:uid', authenticationMiddleware, async (ctx) => {
  let uid = getCurrentUidFromCtx(ctx);
  if (ctx.params.uid !== uid) {
    throw createError(403, '无权限', ErrorCode.ACCESS_DENY);
  }

  let result = await UserProfileService.updateUser({ uid: ctx.params.uid, ...ctx.request.body });
  ctx.response.status = 200;
  ctx.response.body = createSucceedBody('修改成功', result);
});

router.post('/:uid/phone/actions/bind', authenticationMiddleware, async (ctx) => {
  let uid = getCurrentUidFromCtx(ctx);
  if (ctx.params.uid !== uid) {
    throw createError(403, '无权限', ErrorCode.ACCESS_DENY);
  }

  let result = await UserProfileService.bindPhone({ uid: ctx.params.uid, ...ctx.request.body });
  ctx.response.status = 200;
  ctx.response.body = createSucceedBody('绑定成功', result);
});

export default router;